package com.example.gybicycle.controller.wx;

import com.example.gybicycle.common.Log.SysLoging;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.entity.Active;
import com.example.gybicycle.entity.LoginInfo;
import com.example.gybicycle.entity.wx.WXLoginInfo;
import com.example.gybicycle.service.ActiveService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name = "活动")
@RestController
@RequestMapping("/wx/active")
public class WActiveController {

    @Autowired
    private ActiveService activeService;
    @PostMapping("/get")
    @SysLoging("活动管理-查看")
    @Operation(summary = "根据id查看")
    public Result get(@RequestBody Active active,HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        active.setLoginId(info.getId());
        active=activeService.get(active);
        return Result.success(active);
    }
    @PostMapping("/list")
    @Operation(summary =  "列表查看")
     public Result findList(@RequestBody Active page,HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        page.setLoginId(info.getId());
        Page<Active> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->activeService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/join")
    @Operation(summary =  "参与活动")
    public Result join(@RequestBody Active active, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        if(active.getId()==null)
            return Result.fail("请传输活动ID");

        Result result=activeService.joinActive(active.getId(),info.getId());
        return result;
    }
}
